<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28
 * Time: 14:50
 */
namespace Model;

use Shop\Mypdo;

//定义模型
class LoginModel extends Mypdo{

    //实现登录方法
    //$user = 用户名
    //$pass =密码
    public function Alogin($user,$pass){
        if($user&&$pass){

            //查询数据库,验证码
            $sql="select * from s_user where name = '$user'";
            $arr=$this->pdo_sel($sql);
            if (empty($arr)){
                return '用户名不存在';
            }

            if($arr['pass']!=$pass){
                return '密码错误';
            }
            //查询拥有的权限
            $role= str_replace('|',',',$arr['r_id']);
            $sql="select a_id from s_role where id in ($role)";
            $role=$this->pdo_sel($sql,2);//得到多个角色

            $accessid=''; //权限表里面的id
            foreach ($role as $v){
                $accessid.=str_replace('|',',',$v['a_id']);
            }
            //得到所有的权限
            $sql="select * from s_access where id in ($accessid)";
            $access=$this->pdo_sel($sql,2); //得到所有的权限
            $arr['access']=$access;

            //实现登录,保存session
            $_SESSION[SHOP]['user']=$arr;

            //修改 ip 和时间
            $list=array();
            $list['ip']=$_SERVER['REMOTE_ADDR'];
            $list['time']=date("Y-m-d H:i:s",time());
            $this->pdo_upd($list,'s_user'," id = '{$arr['id']}'");

            return false;
        }
    }

    //查询用户数据
    public function AgetUser($name){
        return $this->pdo_sel("select * from s_user where name = '$name'");
    }

    //修改ip和数据
    public function AsetUser($name){
        return $this->pdo_upd(array('ip'=>$_SERVER['REMOTE_ADDR'],'time'=>date("Y-m-d H:i:s",time())),'s_user'," name = '$name'");
    }
}